import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * @author yu
 * @date 2025年04月19日 12:18
 */
public class _02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            int n = sc.nextInt();
            sc.nextLine(); // 消耗换行符
            String s = sc.nextLine().trim();

            char[] a = s.toCharArray();
            char[] b = s.toCharArray();
            Arrays.sort(b);

            List<Integer> diffIndices = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                if (a[i] != b[i]) {
                    diffIndices.add(i);
                    if (diffIndices.size() > 2) break;
                }
            }

            // 处理特殊情况
            if (n == 1) {
                System.out.println("YES");
                continue;
            }

            if (diffIndices.isEmpty()) {
                System.out.println("YES");
            } else if (diffIndices.size() == 2) {
                int i = diffIndices.get(0);
                int j = diffIndices.get(1);
                // 检查是否可以交换且字符不同
                if (a[i] == b[j] && a[j] == b[i] && a[i] != a[j]) {
                    System.out.println("YES");
                } else {
                    System.out.println("NO");
                }
            } else {
                System.out.println("NO");
            }
        }
        sc.close();
    }
}
/*    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            int n = sc.nextInt();
            sc.nextLine(); // 消耗换行符
            String s = sc.nextLine().trim(); // 读取并去除前后空格

            char[] a = s.toCharArray();
            char[] b = s.toCharArray();
            Arrays.sort(b);

            int diffCount = 0;
            for (int i = 0; i < n; i++) {
                if (a[i] != b[i]) {
                    diffCount++;
                    if (diffCount > 2) break;
                }
            }

            // 处理特殊情况：n=1时总是有序
            if (n == 1) {
                System.out.println("YES");
                continue;
            }

            if (diffCount == 0 || diffCount == 2) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        }
        sc.close();
    }*/

